
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Routing Archives - TECHNIG</title>
	<atom:link href="https://www.technig.com/tag/routing/feed/" rel="self" type="application/rss+xml" />
	<link>https://168.138.42.164/tag/routing/amp/</link>
	<description>Gateway for IT Experts and Tech Geeks</description>
	<lastBuildDate>Sat, 23 Jan 2016 20:34:36 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>

<image>
	<url>https://www.technig.com/wp-content/uploads/2020/04/32x32.png</url>
	<title>Routing Archives - TECHNIG</title>
	<link>https://168.138.42.164/tag/routing/amp/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">162720667</site>	<item>
		<title>Laravel 5.2 Basic Routing</title>
		<link>https://www.technig.com/laravel-routing-basic/</link>
					<comments>https://www.technig.com/laravel-routing-basic/#respond</comments>
		
		<dc:creator><![CDATA[Hujatulla Asghari]]></dc:creator>
		<pubDate>Sat, 23 Jan 2016 20:34:36 +0000</pubDate>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[Laravel 5]]></category>
		<category><![CDATA[Laravel Framework]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP framework]]></category>
		<category><![CDATA[Routing]]></category>
		<guid isPermaLink="false">https://www.technig.com/?p=5045</guid>

					<description><![CDATA[<div style="margin-bottom:20px;"></div>
<p>Previously, we learned how to install and configure Laravel framework. And now we are going to continue our topic about how Laravel routing and views works. If you haven&#8217;t install Laravel yet, I recommend you to review our previous topic about how to install laravel 5 on windows, both using composer and windows installer. What is [&#8230;]</p>
<p>The post <a href="https://www.technig.com/laravel-routing-basic/">Laravel 5.2 Basic Routing</a> appeared first on <a href="https://www.technig.com">TECHNIG</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div style="margin-bottom:20px;"></div><p><a href="https://www.technig.com/installing-laravel-5-on-windows/">Previously</a>, we learned how to install and configure <a href="https://laravel.com">Laravel framework</a>. And now we are going to continue our topic about how Laravel routing and views works. If you haven&#8217;t install Laravel yet, I recommend you to review our previous topic about <a href="https://www.technig.com/installing-laravel-5-on-windows/">how to install laravel 5 on windows</a>, both using composer and windows installer.</p>
<h2>What is Laravel routing ?</h2>
<p>Simply, routing is just how Laravel respond to URL&#8217;s request. All Laravel routes are defined in the <em>app/Http/routes.php</em> file, which is automatically loaded by the framework. Routing is the core components of Lararvel framwork. It looks a little complex at first, but once you get comfortable with basics you will be amazed by this feature of Laravel.</p>
<p>Before starting, go to your Laravel root directory and open command line, than write the command &#8220;<em>php artisan serve</em>&#8221; (without quote). This command will run your web server on port <em>8000.</em></p>
<figure id="attachment_5046" aria-describedby="caption-attachment-5046" style="width: 890px" class="wp-caption aligncenter"><a href="https://www.technig.com/wp-content/uploads/2016/01/php-artisan-serve.png" rel="attachment wp-att-5046"><img fetchpriority="high" decoding="async" class="wp-image-5046 size-full" src="https://www.technig.com/wp-content/uploads/2016/01/php-artisan-serve.png" alt="Laravel routing and php artisan serve" width="890" height="449" /></a><figcaption id="caption-attachment-5046" class="wp-caption-text">Laravel routing and php artisan serve</figcaption></figure>
<p>Now you can access your Laravel project at &#8220;<em>http://localhost:8000/</em>&#8220;.</p>
<p>To work with Laravel routs, you should open <em>app<span class="token operator">/</span>Http<span class="token operator">/</span>routes<span class="token punctuation">.</span>php</em> file in your Laravel project and. All Laravel routs are written there.</p>
<p>Imagine if you have: Home, About and a Contact page for your site. The users might look for those page like this;</p>
<p>for Home page:</p>
<pre class="theme:sublime-text lang:php decode:true">http://localhost:8000/</pre>
<p>for About page:</p>
<pre class="theme:sublime-text font:tahoma lang:php decode:true">http://localhost:8000/about
</pre>
<p><span style="line-height: 1.5;">and for Contac page:</span></p>
<pre class="theme:sublime-text lang:php decode:true ">http://localhost:8000/contact</pre>
<p>So in Laravel routs you file, you can write it like this;</p>
<pre class="theme:sublime-text lang:php decode:true">// For Home page
Route::get('/', function () {
    return view('home');
});
// For About page
Route::get('/about', function () {
    return view('about');
});
// For Contact page
Route::get('/about', function () {
    return view('contact');
});</pre>
<p>The above code is just basic way of routing in Laravel.</p>
<p>The Class <em>Route</em> has a static method called get(), which accept two values or arguments. The first one is the directory, and the second one is the function which will be called after the user call the specified directory.</p>
<p>I real world applications, you will not call the views directly from routes, instead you will call the controllers and the controller will call the targeted view.</p>
<h2>Laravel Route Parameters</h2>
<p>Of course, sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user&#8217;s ID from the URL. You may do so by defining route parameters. let&#8217;s do learn it by writing an example.</p>
<p>You might want to capture students name from url:</p>
<pre class="theme:sublime-text lang:php decode:true">// get the parameter of name
    Route::get('students/{name}', function($name) 
    {
        return 'Students Name is ' . $name;
    });
</pre>
<p>Now in your browser, you can access the student name like this &#8220;http://localhost:8000/students/ali&#8221;.</p>
<h2>Laravel Optional Route Parameters</h2>
<p>For this example, let’s say we have a gallery of photos. We also have categories of photos. The category will be optional, if the user set the category, it will show all images from that category, if not it will show all images.</p>
<pre class="theme:sublime-text lang:php decode:true ">// optional category
    Route::get('gallery/{category?}', function($category) 
    {
        // if category is set, show the category
        // if not, then show all
        if ($category)
            return 'This is the ' . $category . ' section.';
        else 
            return 'These are all the photos.';

    });</pre>
<h3>Conclusion</h3>
<p>So, its was just basic and starting of Laravel routing. I hope it has been informative for you, if you have any question of you want say anything about the tutorial feel free to comment it below, and I will join the discussion. 🙂</p>
<p>The post <a href="https://www.technig.com/laravel-routing-basic/">Laravel 5.2 Basic Routing</a> appeared first on <a href="https://www.technig.com">TECHNIG</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.technig.com/laravel-routing-basic/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5045</post-id>	</item>
	</channel>
</rss>
